﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SQLTouch.Service.DataContracts
{
    [DataContract]
    public struct SelectData : IEquatable<SelectData>
    {
        [DataMember]
        private readonly string column;
        [DataMember]
        private readonly AggregateTypeData aggregate;

        public SelectData(string column, AggregateTypeData aggregate)
        {
            this.column = column;
            this.aggregate = aggregate;
        }

        public string Column
        {
            get { return column ?? string.Empty; }
        }

        public AggregateTypeData Aggregate
        {
            get { return aggregate; }
        }

        public override string ToString()
        {
            switch (Aggregate)
            {
                case AggregateTypeData.None:
                    return Column;
                case AggregateTypeData.Count:
                    return "count(*)";
                default:
                    return Aggregate.ToString() + "(" + Column + ")";
            }
        }

        public bool Equals(SelectData other)
        {
            return this.Column == other.Column && this.Aggregate == other.Aggregate;
        }

        public override int GetHashCode()
        {
            return this.Column.GetHashCode() ^ this.Aggregate.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is SelectData)
            {
                return Equals((SelectData)obj);
            }
            else
            {
                return false;
            }
        }

        public static bool operator ==(SelectData left, SelectData right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(SelectData left, SelectData right)
        {
            return !(left == right);
        }
    }
}
